package aom.ju.ss.view.base

import android.app.Activity
import android.os.Bundle
import android.os.Message
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import aom.ju.ss.R
import aom.ju.ss.databinding.ListBinding
import aom.ju.ss.lpInterface.MyHttpCallBack
import aom.ju.ss.mode.YHGoodsData
import aom.ju.ss.utils.Fin
import aom.ju.ss.utils.LogUtils
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.utils.log
import aom.ju.ss.utils.show
import com.google.gson.Gson
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import java.util.*
import kotlin.collections.ArrayList

/**
 * 测试
 *
 */
abstract class BaseListFragment : Fragment(), MyHttpCallBack {
    protected var mContext: Activity? = null
    protected lateinit var pAdapter: PAdapter
    protected var size = 10
    protected var page = 1
    var binding: ListBinding? = null
    protected var container: ViewGroup? = null
    private var activity: Activity? = null
    private var refreshLayout: SwipeRefreshLayout? = null
    protected var isShow = true// 显示网路请求loading
    protected var isHttp = true//是否请求网络
    var isPost = true

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }

        if (binding == null) {
            if (activity == null) {
                try {
                    activity = getActivity()
                } catch (e: Exception) {
                }
            }
            this.container = container
            mContext = getActivity()
            binding = DataBindingUtil.inflate(inflater, R.layout.list, container, false)
            binding!!.mRecycler.layoutManager = LayoutManager()
            pAdapter = PAdapter(
                list, item()
            ) { binding, position ->
                bind(binding, position)
            }
            binding!!.mRecycler.adapter = pAdapter
            binding!!.small.setOnRefreshListener {
                it.finishRefresh(3000)
                page = 1
                list.clear()
                data
                OnRefresh()
            }
            binding!!.small.setOnLoadMoreListener {
                it.finishLoadMore(3000)
                OnLoadMore()
                if (dataSize != 0) {
                    page++
                    data
                } else {
                    "没有更多数据".show
                    it.finishLoadMore()
                }
            }
            if (isHttp) {
                binding!!.small.autoRefresh()
            }
            onBaseCreateView()
        }
        return binding!!.root
    }
    override fun onDestroy() {
        super.onDestroy()
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
    }
    open fun onBaseCreateView() {

    }

    protected open fun OnLoadMore() {}
    protected open fun OnRefresh() {}

    protected open fun item(): Int {
        return -1
    }

    protected var dataSize = 0


    private var list: MutableList<Any> = ArrayList()

    fun getList(): MutableList<Any> {
        return list
    }

    fun getListString(): String {
        var a = "";
        for (asc in list) {
            if (asc.toString() != "") {
                if (a != "") {
                    a += ","
                }
                a += asc
            }

        }
        return a
    }

    fun setList(ls: List<Any>?) {
        val listls = arrayListOf<Any>()
        if (ls != null) {
            listls.addAll(ls)
        }
        dataSize = listls.size
        list.addAll(listls)

        pAdapter.notifyDataSetChanged()
        binding!!.noData.visibility = if (list.isEmpty()) View.VISIBLE else View.GONE
    }

    open fun bind(binding: ViewDataBinding, position: Int) {}

    protected val data: Unit
        get() {
            if (!isHttp) {
                "本地模式".log
                return
            } else {
                "网络模式".log
            }

            val ok = OKHttpUtils(activity, isShow)

            LogUtils.e("key()=" + Gson().toJson(key()))

            LogUtils.e("value()=" + Gson().toJson(value()))

            ok.SetApiUrl(api())
            ok.SetKey(*key())
            ok.SetValue(*value())
            if (isPost) {
                ok.POST(this@BaseListFragment)
            } else {
                ok.GET(this@BaseListFragment)
            }


//            GetTokenUtils(activity, api(), isShow)
//                .setApiVersion(apiVersion)
//                .Get(object : GetTokenBack {
//                    override fun ok(token: String, ok: OKHttpUtils) {
//                        apiToken = token
//                        ok.SetApiUrl(api()).SetKey(*key()).SetValue(*value())
//                        if (isPost) {
//                            ok.POST(this@BaseListFragment)
//                        } else {
//                            ok.GET(this@BaseListFragment)
//                        }
//                    }
//                })

        }

    protected open fun value(): Array<String> {
        return arrayOf()
    }

    protected open fun key(): Array<String> {

        return arrayOf()
    }

    protected open fun api(): String {
        return ""
    }


    protected open fun LayoutManager(): RecyclerView.LayoutManager? {
        return LinearLayoutManager(activity)
    }

    fun ref(refreshLayout: SwipeRefreshLayout?) {
        this.refreshLayout = refreshLayout
        page = 1
        list.clear()
        data
    }

    override fun ok(jsonString: String, httpTY: String) {
        if (refreshLayout != null) {
            refreshLayout!!.isRefreshing = false
        }
        binding!!.small.Fin()
//
//        binding!!.noData.visibility = if (list.size == 0) View.VISIBLE else View.GONE
    }

    override fun error(e: String?, jsonString: String?, httpTY: String?) {
        binding!!.small.Fin()
    }

    @Subscribe
    open fun callBack(event: Message) {
    }
}